import {SysRoleMenuAdd, SysRoleMenuData, SysRoleMenuDetail, SysRoleMenuParams} from "@/services/system/role/menu/type";
import {request} from "@@/plugin-request/request";
import download from "@/utils/download";


export const sysRoleMenuPageList = async (sysRoleMenuParams: SysRoleMenuParams) => {
    return request<{
        data: {
            records: SysRoleMenuData[],
            total: number,
        }
        success: boolean
    }>('/api/sys/role/menu/list', {
        method: 'POST',
        data: sysRoleMenuParams
    });
}


export const sysRoleMenuUpdate = async (sysRoleMenuAdd: SysRoleMenuAdd) => {
    return request<{
        data: string;
    }>('/api/sys/role/menu/update', {
        method: 'POST',
        data: sysRoleMenuAdd
    });
}

export const sysRoleMenuAdd = async (sysRoleMenuAdd: SysRoleMenuAdd) => {
    return request<{
        data: string;
    }>('/api/sys/role/menu/add', {
        method: 'POST',
        data: sysRoleMenuAdd
    });
}

export const sysRoleMenuRemove = async ( ids: (number | string)[] ) => {
    return request<{
        data: string;
    }>('/api/sys/role/menu/remove', {
        method: 'POST',
        data: {
            ids
        }
    });
}

export const sysRoleMenuDetail = async (id: number) => {
    return request<{
        data: SysRoleMenuDetail;
    }>('/api/sys/role/menu/detail', {
        method: 'POST',
        data: {
            id
        }
    });
}

export const sysRoleMenuImport = async (formData: FormData) => {
    return request<{
        data: string;
    }>('/api/sys/role/menu/import', {
        method: 'POST',
        data: formData,
        requestType: "form"
    });
}


const sysRoleMenuExport = async (ids: (number | string)[]) => {
    return request<{
        data: string;
    }>('/api/sys/role/menu/export', {
        method: 'POST',
        data: {ids},
        responseType: 'blob',
        getResponse: true,
    });
}

export const downloadSysRoleMenuExport = async (ids: (number | string)[]) => {
    await sysRoleMenuExport(ids).then(({data, response}) => {
        download(data, response)
    });
}


const sysRoleMenuExportTemplate = async () => {
    return request<{
        data: string;
    }>('/api/sys/role/menu/export/template', {
        method: 'POST',
        responseType: 'blob',
        getResponse: true,
    });
}

export const downloadSysRoleMenuExportTemplate = async () => {
    await sysRoleMenuExportTemplate().then(({data, response}) => {
        download(data, response)
    });
}




